/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.net.URL;

import javax.swing.Box;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.core.Version;
import org.aergia.vinny.database.pojo.VinnyOption;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.engine.Engine;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.sound.Sound;
import org.aergia.vinny.gui.util.FontUtil;
import org.aergia.vinny.util.IconUtil;

/**
 * @author Fred
 */
public class VinnyFrame extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	JLabel result;
	JTextField scanField;
	private JMenuItem aboutMenuItem;
	private JLabel card;
	private final VinnyController controller;
	private JMenu fileMenu;
	private JMenu helpMenu;
	private JScrollPane jScrollPane0;
	private final String location;
	private JMenuItem loginMenuItem;
	private JMenuBar mainMenuBar;
	private final String productName;
	private final Timer resultTimer;
	private final Timer scannedTimer;
	private Sound scanSound;
	private JLabel sponsor;
	private JLabel sponsorText;
	private JLabel welcome;

	/**
	 * @param controller
	 * @param productName
	 * @param location
	 */
	public VinnyFrame(final VinnyController controller, final String productName, final String location) {
		this.controller = controller;
		this.productName = productName;
		this.scannedTimer = new Timer(1000, this);
		this.resultTimer = new Timer(5000, this);
		this.location = location;
		this.initComponents();
		this.setIconImage(IconUtil.getIconImage("/images/16/trackin_it.png"));
		this.controller.setVinnyFrame(this);
	}

	@Override
	public void actionPerformed(final ActionEvent e) {
		if (e.getSource() == this.scanField) {
			if (this.scanSound != null) {
				this.scanSound.play();
			}
			this.controller.cardScanned(this.scanField.getText());
			this.scannedTimer.start();
		} else if (e.getSource() == this.aboutMenuItem) {
			final JDialog aboutDialog = new AboutDialog(this, this.productName);
			aboutDialog.setVisible(true);
		} else if (e.getSource() == this.loginMenuItem) {
			final LoginDialog loginDialog = new LoginDialog(this, this.controller);
			loginDialog.setLocationRelativeTo(this);
			loginDialog.setVisible(true);
			final VinnyUser user = loginDialog.getUser();
			if (user != null) {
				final UserFrame userFrame = new UserFrame(this.controller, user);
				userFrame.setLocationRelativeTo(this);
				userFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				userFrame.setVisible(true);
			}
		} else if (e.getSource() == this.scannedTimer) {
			this.scannedTimer.stop();
			this.scanField.setText("");
		} else if (e.getSource() == this.resultTimer) {
			this.resultTimer.stop();
			this.result.setText("");
		}
	}

	public void showResult(final ApiResult<?> apiResult) {

		if (apiResult.isError()) {
			this.resultTimer.setInitialDelay(5000);
			this.result.setText(apiResult.getText());
			this.result.setForeground(Color.red);
		} else {
			this.resultTimer.setInitialDelay(5000);
			this.result.setText("Successful scan");
			this.result.setForeground(Color.black);
		}
		this.resultTimer.start();
	}

	private JMenuItem getAboutMenuItem() {
		if (this.aboutMenuItem == null) {
			this.aboutMenuItem = new JMenuItem();
			this.aboutMenuItem.setMnemonic('A');
			this.aboutMenuItem.setIcon(IconUtil.getIcon("/images/16/note.png"));
			this.aboutMenuItem.setText("About");
			this.aboutMenuItem.addActionListener(this);
		}
		return this.aboutMenuItem;
	}

	private JLabel getCard() {
		if (this.card == null) {
			this.card = new JLabel();
			this.card.setHorizontalAlignment(SwingConstants.CENTER);
			this.card.setText("Please scan your \"" + this.productName + "\" card.");
			this.card.setFont(FontUtil.getTitleFont());
		}
		return this.card;
	}

	private JMenu getFileMenu() {
		if (this.fileMenu == null) {
			this.fileMenu = new JMenu();
			// this.fileMenu.setText("File");
			this.fileMenu.setMnemonic(KeyEvent.VK_F);
			this.fileMenu.setIcon(IconUtil.getIcon("/images/16/settings.png"));
			this.fileMenu.add(this.getLoginMenuItem());
		}
		return this.fileMenu;
	}

	private JMenu getHelpMenu() {
		if (this.helpMenu == null) {
			this.helpMenu = new JMenu();
			// this.helpMenu.setText("Help");
			this.helpMenu.setIcon(IconUtil.getIcon("/images/16/help.png"));
			this.helpMenu.setMnemonic(KeyEvent.VK_H);
			this.helpMenu.add(this.getAboutMenuItem());
		}
		return this.helpMenu;
	}

	private JScrollPane getJScrollPane0() {
		if (this.jScrollPane0 == null) {
			this.jScrollPane0 = new JScrollPane();
			this.jScrollPane0.setViewportView(this.getResult());
			this.jScrollPane0.setBorder(new EmptyBorder(0, 0, 0, 0));
		}
		return this.jScrollPane0;
	}

	private JMenuItem getLoginMenuItem() {
		if (this.loginMenuItem == null) {
			this.loginMenuItem = new JMenuItem();
			this.loginMenuItem.setText("Login");
			this.loginMenuItem.setIcon(IconUtil.getIcon("/images/16/security.png"));
			this.loginMenuItem.setMnemonic(KeyEvent.VK_L);
			this.loginMenuItem.addActionListener(this);
		}
		return this.loginMenuItem;
	}

	private JMenuBar getMainMenuBar() {
		if (this.mainMenuBar == null) {
			this.mainMenuBar = new JMenuBar();
			this.mainMenuBar.add(this.getFileMenu());
			this.mainMenuBar.add(Box.createHorizontalGlue());
			this.mainMenuBar.add(this.getHelpMenu());
		}
		return this.mainMenuBar;
	}

	private JLabel getResult() {
		if (this.result == null) {
			this.result = new JLabel();
			this.result.setText("");
			this.result.setHorizontalAlignment(SwingConstants.CENTER);
			this.result.setVerticalAlignment(SwingConstants.TOP);
			this.result.setFont(new Font("Dialog", Font.PLAIN, 18));
		}
		return this.result;
	}

	private JTextField getScanField() {
		if (this.scanField == null) {
			this.scanField = new JTextField();
			this.scanField.setText("");
			this.scanField.addActionListener(this);
			this.scanField.setFont(new Font("Dialog", Font.PLAIN, 24));
		}
		return this.scanField;
	}

	private JLabel getSponsor() {
		if (this.sponsor == null) {
			this.sponsor = new JLabel();
			this.sponsor.setIcon(IconUtil.getIcon("/images/64/nv_volunteers.png"));
			this.sponsor.setHorizontalTextPosition(SwingConstants.RIGHT);
		}
		return this.sponsor;
	}

	private JLabel getSponsorText() {
		if (this.sponsorText == null) {
			this.sponsorText = new JLabel();
			this.sponsorText.setFont(new Font("Dialog", Font.ITALIC, 12));
			this.sponsorText.setText("Brought to you by " + this.controller.getOption(VinnyOption.SERVICE_OF, Engine.DEFAULT_SERVICE_OF) + ".");
			this.sponsorText.setHorizontalTextPosition(SwingConstants.RIGHT);
		}
		return this.sponsorText;
	}

	private JLabel getWelcome() {
		if (this.welcome == null) {
			this.welcome = new JLabel();
			this.welcome.setFont(new Font("Dialog", Font.BOLD, 24));
			this.welcome.setHorizontalAlignment(SwingConstants.CENTER);
			this.welcome.setText("Welcome to the " + this.location);
		}
		return this.welcome;
	}

	private void initComponents() {

		final URL clipUrl = VinnyFrame.class.getResource("/sounds/blip.wav");
		if (clipUrl != null) {
			this.scanSound = new Sound(clipUrl);
			try {
				this.scanSound.initialize();
			} catch (final Exception e) {
				this.scanSound = null;
				e.printStackTrace();
			}
		}

		this.setTitle(this.productName + " (" + Version.getVersion() + ")");
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setFont(new Font("Dialog", Font.PLAIN, 12));
		this.setForeground(Color.black);

		final JPanel northPanel = new JPanel(new BorderLayout(5, 5));
		northPanel.add(this.getWelcome(), BorderLayout.NORTH);
		northPanel.add(this.getCard(), BorderLayout.SOUTH);

		final JPanel centerPanel = new JPanel(new BorderLayout(5, 5));
		final JPanel panel = new JPanel(new BorderLayout());
		panel.add(new JLabel("          "), BorderLayout.WEST);
		panel.add(this.getScanField(), BorderLayout.CENTER);
		panel.add(new JLabel("          "), BorderLayout.EAST);
		centerPanel.add(panel, BorderLayout.NORTH);
		centerPanel.add(this.getJScrollPane0(), BorderLayout.CENTER);

		final JPanel southPanel = new JPanel(new BorderLayout());
		southPanel.add(this.getSponsor(), BorderLayout.NORTH);
		southPanel.add(this.getSponsorText(), BorderLayout.SOUTH);

		this.add(northPanel, BorderLayout.NORTH);
		this.add(centerPanel, BorderLayout.CENTER);
		this.add(southPanel, BorderLayout.SOUTH);

		this.setJMenuBar(this.getMainMenuBar());
		this.setSize(640, 330);
	}

}
